package com.sunny.ihealth.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.sunny.ihealth.entity.app.SPUserInfo;
import com.sunny.ihealth.entity.bmob.UserInfo;

import cn.bmob.v3.datatype.BmobFile;

/**
 * SharedPreferences 配置文件帮助类
 * 可以快速获取修改软件配置
 * Created by Sunny  An on 2016/5/15.
 */
public class PreferencesHelper {

    public static final String SP_FILE_NAME = "user_data";
    public static final String SP_MODE = "mode";
    public static final String SP_USERNAME = "username";
    public static final String SP_PASSWORD = "password";
    public static final String SP_NICKNAME = "nickname";
    public static final String SP_SIGNATURE = "signature";
    public static final String SP_GENDER = "gender";
    public static final String SP_HEAD_FILE_NAME = "headFileName";
    public static final String SP_HEAD_URL = "headUrl";

    /**
     * 第一次使用，会显示欢迎界面
     */
    public static final int MODE_FIRST_USE = 0;
    /**
     * 游客模式，会进入主界面，不提示登陆
     */
    public static final int MODE_VISITOR = 1;
    /**
     * 注册用户模式
     */
    public static final int MODE_REGISTER_USER = 2;

    private Context context;

    public PreferencesHelper(Context context) {
        this.context = context;
    }

    /**
     * 获取启动模式
     */
    public int getMode() {
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(SP_MODE, MODE_FIRST_USE);
    }

    /**
     * 设置启动模式
     *
     */
    public void setMode(int mode) {
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        sp.edit().putInt(SP_MODE, mode).apply();
    }

    /**
     * 清除用户本地资料
     */
    public void clearUserInfo() {
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        sp.edit()
                .remove(SP_USERNAME)
                .remove(SP_PASSWORD)
                .remove(SP_NICKNAME)
                .remove(SP_SIGNATURE)
                .remove(SP_GENDER)
                .remove(SP_HEAD_FILE_NAME)
                .remove(SP_HEAD_URL)
                .apply();
    }

    /**
     * 设置用户账号密码
     *
     */
    public void setUserAccount(String username, String password) {
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        sp.edit()
                .putString(SP_USERNAME, username)
                .putString(SP_PASSWORD, password)
                .apply();
    }


    /**
     * 保存用户资料
     *
     */
    public void setUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(SP_NICKNAME, userInfo.getNickname());
        editor.putString(SP_SIGNATURE, userInfo.getSignature());
        editor.putBoolean(SP_GENDER, userInfo.getGender());
        BmobFile bmobFile = userInfo.getHeadFile();
        if (bmobFile != null && !TextUtils.isEmpty(bmobFile.getFilename())) {
            editor.putString(SP_HEAD_FILE_NAME, bmobFile.getFilename());
            editor.putString(SP_HEAD_URL, bmobFile.getUrl());
        } else {
            editor.remove(SP_HEAD_FILE_NAME);
            editor.remove(SP_HEAD_URL);
        }
        editor.apply();
    }

    /**
     * 获取用户账号，资料
     *
     */
    public SPUserInfo getSPUserInfo() {
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        String username = sp.getString(SP_USERNAME, "");
        String password = sp.getString(SP_PASSWORD, "");
        String nickname = sp.getString(SP_NICKNAME, "");
        String signature = sp.getString(SP_SIGNATURE, "");
        String path = sp.getString(SP_HEAD_FILE_NAME, "");
        String url = sp.getString(SP_HEAD_URL, "");
        boolean gender = sp.getBoolean(SP_GENDER, true);
        if (TextUtils.isEmpty(username)) {
            return null;
        }
        SPUserInfo spUserInfo = new SPUserInfo();
        spUserInfo.setUsername(username);
        spUserInfo.setPassword(password);
        spUserInfo.setNickname(nickname);
        spUserInfo.setSignature(signature);
        spUserInfo.setHeadFileName(path);
        spUserInfo.setGender(gender);
        spUserInfo.setHeadUrl(url);
        return spUserInfo;

    }


}
